package com.future.lock.mall.tasks;

import com.future.lock.common.http.HttpUtils;
import com.future.lock.mall.events.Events;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yzh on 2018/1/6.
 */

public class SetOrderStatusTask {
    // 已付款
    public static final int STATUS_PAIED = 1;
    // 确认收货
    public static final int STATUS_CONFIRMED = 3;
    // 申请退款
    public static final int STATUS_REFUND = 4;
    // 取消订单
    public static final int STATUS_CANCEL = 10;

    public static void setOrderStatus(String token, String orderId, int status, Runnable... runnable) {
        HttpUtils.getHttpApi()
                .setOrderStatus(token, orderId, status)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        try {
                            if (new JSONObject(response.body()).optInt("success") == 1) {
                                // 发生订单状态改变事件
                                EventBus.getDefault()
                                        .post(new Events.OnOrderStatusUpdateEvent());
                                runnable[0].run();
                            }
                            else {
                                runnable[1].run();
                            }
                        }
                        catch (Exception e) {
                            if (runnable != null && runnable.length > 1) {
                                runnable[1].run();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        if (runnable != null && runnable.length > 1) {
                            runnable[1].run();
                        }
                    }
                });
    }
}
